<template>
  <div>
    <div class="btn-text" @click="getProcess">{{ btnText }}</div>
    <el-dialog
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :append-to-body="true"
      class="open-invoice-common-dialog large-common-dialog-size"
      width="640px">
      <Title title="成本明细" font-size="16px" style="margin-bottom: 10px" />
      <div class="dialog-main">
        <el-table
          v-loading="loading"
          :data="processData"
          :max-height="tableHeight"
          element-loading-text="加载中..."
          empty-text="暂无数据"
          header-cell-class-name="order-data-header-cell"
          fit
          hightlight-current-row>
          <el-table-column align="left" label="成本项" width="200">
            <template slot-scope="scope">
              <ToolTip :content="scope.row.costItemName"/>
            </template>
          </el-table-column>
          <el-table-column align="left" label="含税成本" width="200">
            <template slot-scope="scope">
              <div>
                <span>{{ scope.row.taxCost / 100 | moneyFilter }}</span>
                <span v-if="scope.row.costType === 1">
                  （<span>ID：</span>
                  <span v-if="scope.row.platId || scope.row.platId === 0">{{ scope.row.platId }}</span>
                  <span v-else>--</span>）
                </span>
                <span v-else-if="scope.row.costType === 4" >
                  （<span>分成比例：</span>
                  <span v-if="scope.row.dividePercent || scope.row.dividePercent === 0">{{ scope.row.dividePercent }}</span>
                  <span v-else>--</span>）
                </span>
                <span v-else-if="scope.row.costType === 5 && scope.row.autoCost" >
                  （自动）
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="left" label="供应商" width="200">
            <template slot-scope="scope">
              <ToolTip :content="scope.row.supplierName"/>
            </template>
          </el-table-column>
          <el-table-column align="left" label="可抵扣税率" width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.deductionRate || scope.row.deductionRate === 0">{{ scope.row.deductionRate }}%</span>
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column align="left" label="成本状态" width="90">
            <template slot-scope="scope">
              <div>{{ scope.row.settlementStatus | settlementStatusFilter }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrderCost } from '@/api/config';
import Title from '@/components/Title';
import { settlementStatus } from '@/views/cost/model/selectData';
import { moneyFilter } from '@/filters/number';
export default {
  components: { Title },
  filters: {
    moneyFilter,
    statusFilter: (state) => {
      const num = Number(state);
      // 0:成本选择 1:订单选择 2:结算审批 3:结算支付 4: 结算完成
      const arr = ['待结算', '结算完成', '结算中'];
      return arr[num] || '--';
    },
    settlementStatusFilter: (state) => {
      // settlementStatus
      const res = settlementStatus.find(i => i.value === state);
      if (res) {
        return res.label;
      }
      return '--';
    }
  },
  props: {
    btnText: {
      type: String,
      default: '查看结算'
    },
    costList: {
      type: Array,
      default: () => {
        return [];
      }
    },
    orderId: {
      type: String | Number,
      default: ''
    }
  },
  data() {
    return {
      processData: [],
      dialogVisible: false,
      loading: false,
      tableHeight: ''
    };
  },
  methods: {
    getProcess() {
      const clientHeight = window.innerHeight;
      this.tableHeight = (clientHeight * 4) / 5;
      this.dialogVisible = true;
      this.loading = true;
      getOrderCost({ id: this.orderId }).then(res => {
        this.loading = false;
        if (res.code === 200) {
          this.processData = res.data;
        }
      }).catch(() => {
        this.loading = false;
      });
    },
    handleClose() {
      this.dialogVisible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.btn-text {
  font-size: 14px;
  color: #406EFF;
  cursor: pointer;
  &:hover {
    color: #1849e7;
  }
}
.supplier-status-list {
  display: flex;
  margin-top: 20px;
  flex-wrap: wrap;
  .supplier-status-item {
    width: 50%;
    margin-bottom: 20px;
    padding-right: 10px;
  }
}
</style>
